//
// Created by hitfy on 2018/5/7.
//

#include <bits/stdc++.h>
#define Length 100000
using namespace std;
int main() {
    int N, M;
    string str;
    int illegal[26][26] = {0};
    int F[Length];
    int index[26];
    memset(index, -1, sizeof(index));
//    for (int i = 0; i < 26; i++)//索引26个字母上一次出现的位置，-1代表没出现过
//        index[i] = -1;
    cin >> N >> str >> M;
    for(int i = 0; i < M; i++){
        char c1, c2;
        cin >> c1 >> c2;
        illegal[c1-'a'][c2-'a'] = illegal[c2-'a'][c1-'a'] = 1;
    }
    int max = INT_MIN;
    for (int i = 0; i < N; i++) {
        F[i] = 1;
        for (char j = 0; j < 26; j++) {
            if (index[j] < 0 || illegal[j][str[i]-'a'] == 1 || illegal[str[i]-'a'][j] == 1)
                continue;
            if (F[index[j]] + 1 > F[i])
                F[i] = F[index[j]] + 1;
        }
        index[str[i] - 'a'] = i;
        max = max > F[i] ? max : F[i];//！！！不一定是取最后一个字母的值，中间可能有更大的值
    }
    cout << N - max;
    return 0;
}

/**
题目1 : Composition
时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

Alice writes an English composition with a length of N characters. However, her teacher requires that M illegal pairs of characters cannot be adjacent, and if 'ab' cannot be adjacent, 'ba' cannot be adjacent either.

In order to meet the requirements, Alice needs to delete some characters.

Please work out the minimum number of characters that need to be deleted.
输入

The first line contains the length of the composition N.

The second line contains N characters, which make up the composition. Each character belongs to 'a'..'z'.

The third line contains the number of illegal pairs M.

Each of the next M lines contains two characters ch1 and ch2,which cannot be adjacent.

For 20% of the data: 1 ≤ N ≤ 10

For 50% of the data: 1 ≤ N ≤ 1000

For 100% of the data: 1 ≤ N ≤ 100000, M ≤ 200.
输出

One line with an integer indicating the minimum number of characters that need to be deleted.
样例提示

Delete 'a' and 'd'.
样例输入

    5
    abcde
    3
    ac
    ab
    de

样例输出

    2

*/

/**
本题最暴力的解法就是枚举哪些字符被删掉。枚举的复杂度是O(2^N)，再加上O(N)的判断，总复杂度是O(N2^N)。

比较容易想到的是O(N^2)的DP。

由于删掉最少等价于留下最多，所以我们可以用f[i]表示最后留下一个字符是S[i]时，最多留下多少个字符。

要求f[i]，我们只需要枚举S[i]之前的一个字符是哪个，并且从中选择合法并且最优的解：

f[i] = max{f[j]} + 1, for j = 1 .. i-1且S[j]S[i]不是"illegal pair"。

以上的DP解法是O(N^2)的，仍有优化的空间。

我们求f[i]时，需要枚举之前所有的位置j，但实际上之前的字符只有'a'~'z'26种可能。对于相同的字符，我们只用考虑最后一次它出现的位置，之前的位置一定不是最优。

例如，假设s[2] = s[5] = s[8] = 'h'，那么当i>8时，我们求f[i]根本不用考虑j=2和j=5的情况，这两种情况即便合法也不会比j=8更优。

于是我们每次求f[i]只需要考虑'a'~'z'最后一次出现的位置。复杂度可以优化到O(26N)。
 */
